#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMetaObject>

int iThreadStop;



cWidget::cWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    qcThreadObj = new QThread();
    cSerialObj = new CSerialObject();
    cSerialObj->moveToThread(qcThreadObj);
    connect(qcThreadObj, &QThread::started, cSerialObj, &CSerialObject::slot_init); //开始线程的信号触发子线程的槽函数 slot_init()在子线程中开始运行

    connect(cSerialObj, &CSerialObject::sign_send_frame, this, &cWidget::slot_show_frame);

    connect(qcThreadObj, &QThread::finished, cSerialObj, &CSerialObject::slot_colse);   //退出时关闭打开的串口
    connect(qcThreadObj, &QThread::finished, cSerialObj, &CSerialObject::deleteLater);  //退出时释放对象资源
    connect(qcThreadObj, &QThread::finished, qcThreadObj, & QThread::deleteLater);      //退出时释放线程资源

    qcThreadObj->start();

    //cmSerial.moveToThread(&qcChildThread);
    //connect(ui->pushbtnOpen, &QPushButton::clicked, &cmSerial, &CMSerial::ToWork);
    //connect(&qcChildThread, &QThread::started, &cmSerial, &CMSerial::ToWork);
    //connect(this, &cWidget::SSS, &cmSerial, &CMSerial::ToWork);
    //qcChildThread.start();

    /*
    iThreadStop =2;

    for(QSerialPortInfo info: QSerialPortInfo::availablePorts()) //读取PC所有的串口号
    {
        qslSerialName << info.portName();
    }
    qDebug() << qslSerialName;
    ui->comboxSerialName->addItems(qslSerialName); //在下来栏中显示所有的串口号

    qslBaudRate << "9600" << "19200" << "38400" << "115200";
    ui->comboxBaudRate->addItems(qslBaudRate);

    connect(ui->pushbtnOpen, &QPushButton::clicked, this, &cWidget::SerialOpen); //关联打开按钮
    connect(ui->pushbtnCancel, &QPushButton::clicked, this, &cWidget::SerialCancel);

    connect(this, &cWidget::SSS, &cmSerial, &CMSerial::ToStop);
    connect(this, &cWidget::SSS2, &cmSerial, &CMSerial::ToStop2);
*/
    connect(ui->pushbtnOpen, &QPushButton::clicked, this, &cWidget::SerialOpen); //关联打开按钮
    connect(ui->pushbtnCancel, &QPushButton::clicked, this, &cWidget::SerialCancel);

    connect(this, &cWidget::sign_send_frame, cSerialObj, &CSerialObject::slot_recv_frame);

    testarray = QByteArray::fromHex("1234567890");
}

cWidget::~cWidget()
{
    delete ui;
}

int cWidget::GetSerialBaudRate()
{
    return ui->comboxBaudRate->currentText().toUInt();
}

QString cWidget::GetSerialName()
{
    return ui->comboxSerialName->currentText();
}


void cWidget::SerialOpen()
{

    qcChildThread.start();

    emit SSS();
    emit SSS2();
    emit sign_send_frame(testarray);

    qDebug()<<"sys id="<<QThread::currentThreadId();

    //QThread::sleep(5);
    //delete &cmSerial;
    /*
    qDebug() << "open";
    pcUartInst = new cUart(this); //创建串口

    connect(pcUartInst, &cUart::SerialThreadFin, this, &cWidget::SerialThreadFinDone);
    connect(this, &cWidget::destroyed, this , &cWidget::StopThread);

    pcUartInst->start();
    */

}

void cWidget::SerialThreadFinDone()
{

}

void cWidget::StopThread()
{
    pcUartInst->quit();
    pcUartInst->wait();
}

void cWidget::SerialCancel()
{
    iThreadStop =1;
    qDebug() << "cancel" << iThreadStop;

    if(cSerialObj->qcSerialPortObj->isOpen())//如果串口已经打开了 先给他关闭了
    {
        cSerialObj->qcSerialPortObj->clear();
        cSerialObj->qcSerialPortObj->close();
    }

    //QMetaObject::invokeMethod(&qcChildThread, "requestStop");
    //qcChildThread.terminate();
    //qcChildThread.wait();
    //QMetaObject::invokeMethod(&cmSerial, "ToStop",Qt::QueuedConnection); //invokeMethod 好像只能调用槽函数，待确定
    //QMetaObject::invokeMethod(&cmSerial, "ToStop",Qt::DirectConnection); //用DirectConnection 等同于调用了cwork.ToStop(); 因为这个参数是认为cwork.ToStop在主线程中调用

    //QMetaObject::invokeMethod(&cmSerial, "ToStop");
    //cmSerial.ToStop();
    emit SSS();
    gbSW =false;
    //qcChildThread.quit();
    //qcChildThread.wait();

    //this->close();
}

void cWidget::slot_show_frame(QByteArray data)
{
    QString hexString;
    for (char byte : data) {
        hexString.append(QString::number(static_cast<unsigned char>(byte), 16).rightJustified(2, '0').toUpper());
        hexString.append(" "); // 在每个字节之间添加空格，以便更容易阅读
    }
    hexString.trimmed(); // 移除末尾的空格
    //this->ui->textEditShowData->clear();
    this->ui->textEditShowData->append(hexString);
    //this->ui->textEditShowData->setPlainText(hexString);
}



